'''
Numerical heat transfer simulation
Created on 2010-11-3

@author: yun_hua
'''

import numpy as N

class Grid(object):
    def __init__(self):
        pass

    def rectangle(cls, l, m, lx, mx):
        """ Generate a 2-D rectangle grid zone in regular way. A class method.
            l: the x-axis length.
            m: the y-axis (r-axis) length.
            lx: l direction grid number.
            mx: m direction grid number.
        """
        if l < 0.0 or m < 0.0:
            raise ValueError, "l or m is negative."
        lx = int(lx)
        mx = int(mx)
        if lx <= 2 or mx <= 2:
            raise ValueError, "lx or mx space is zero or negative"
        
        grid = N.zeros((mx + 2, lx + 2, 2), dtype=N.float64) # +2 means two boundaries in one axis.
        lpos = N.zeros(lx + 2)
        lpos[1:] = N.linspace(0., l, lx + 1) + l / lx / 2. # inner points
        lpos[-1] = l
        mpos = N.zeros(mx + 2)
        mpos[1:] = N.linspace(0., m, mx + 1) + m / mx / 2. # inner points
        mpos[-1] = m
        grid[:, :, 0] = N.tile(lpos, (mx + 2, 1))
        grid[:, :, 1] = N.tile(mpos, (lx + 2, 1)).T 
        return grid
    
    rectangle = classmethod(rectangle)

